﻿using System;

namespace NodeJS.net
{
    /// <summary>
    /// This class is used to create a TCP or UNIX server. A server is a net.Socket that can listen for new incoming connections.
    /// </summary>
    public class Server : Socket
    {
        /// <summary>
        /// Begin accepting connections on the specified port and host.
        /// </summary>
        /// <param name="port">A port value of zero will assign a random port.</param>
        /// <param name="host">If the host is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).</param>
        /// <param name="backlog">Backlog is the maximum length of the queue of pending connections. The actual length will be determined by your OS through sysctl settings such as tcp_max_syn_backlog and somaxconn on linux. The default value of this parameter is 511 (not 512).</param>
        /// <param name="callback">This function is asynchronous. When the server has been bound, 'listening' event will be emitted. The last parameter callback will be added as an listener for the 'listening' event.</param>
        public virtual void Listen(int port, string host = null, int backlog = 0, Action callback = null)
        {
        }

        /// <summary>
        /// Start a UNIX socket server listening for connections on the given path.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="callback">This function is asynchronous. When the server has been bound, 'listening' event will be emitted. The last parameter callback will be added as an listener for the 'listening' event.</param>
        public virtual void Listen(string path, Action callback = null)
        {
        }

        /// <summary>
        /// Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event.
        /// </summary>
        /// <param name="callback"> Optionally, you can pass a callback to listen for the 'close' event.</param>
        public virtual void Close(Action callback = null)
        {
        }

        /// <summary>
        /// Returns the bound address, the address family name and port of the server as reported by the operating system. Useful to find which port was assigned when giving getting an OS-assigned address.
        /// Don't call server.address() until the 'listening' event has been emitted.
        /// </summary>
        /// <returns>Returns an object with three properties, e.g. { port: 12346, family: 'IPv4', address: '127.0.0.1' }</returns>
        public override SocketAddress Address()
        {
            return null;
        }

        /// <summary>
        /// Set this property to reject connections when the server's connection count gets high.
        /// It is not recommended to use this option once a socket has been sent to a child with child_process.fork().
        /// </summary>
        public int MaxConnections;


        /// <summary>
        /// Asynchronously get the number of concurrent connections on the server. Works when sockets were sent to forks.
        /// </summary>
        /// <param name="callback">Callback should take two arguments err and count.</param>
        public void GetConnections(Action<Exception, int> callback)
        {
        }

        /// <summary>
        /// Emitted when the server has been bound after calling server.listen.
        /// </summary>
        [EvalAtCompile(Value = "listening")]
        public const NodeEvent LISTENING = null;
    }
}
